package com.quasar.hpatient.module.comm_regist;

import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.quasar.hpatient.R;

import lib.quasar.base.frame.BaseActivity;
import lib.quasar.context.BaseApp;
import lib.quasar.widget.timer.TimerView;
import lib.quasar.widget.warning.WarningView;

/**
 * description: 用户注册
 * created by kalu on 2018/3/22 16:57
 */
public class RegistActivity extends BaseActivity<RegistPresenter> implements RegistView {

    public final static int RESULT_COMPILE = 1005;

    public final static String NAME = "NAME";
    public final static String NAME_REGIST = BaseApp.getResource().getString(R.string.comm_regist_title);
    public final static String NAME_FORGET = BaseApp.getResource().getString(R.string.comm_forget_title);

    @Override
    public int initView() {
        return R.layout.activity_comm_regist;
    }

    @Override
    public void initDataLocal() {

        final String stringExtra = getIntent().getStringExtra(NAME);
        final TextView title = findViewById(R.id.regist_title);
        setText(title, TextUtils.isEmpty(stringExtra) ? NAME_REGIST : stringExtra);

        findViewById(R.id.activity_comm_regist_login).setOnClickListener(v -> onBackPressed());

        findViewById(R.id.activity_comm_regist_eye).setOnClickListener(v -> changeInputType(R.id.activity_regist_password1, R.id.activity_comm_regist_eye));
        findViewById(R.id.activity_comm_regist_eye_twice).setOnClickListener(v -> changeInputType(R.id.activity_regist_password2, R.id.activity_comm_regist_eye_twice));

        findViewById(R.id.activity_comm_regist_code).setOnClickListener(v -> {
            final View button = findViewById(R.id.activity_comm_regist_code);
            final TimerView timer = findViewById(R.id.activity_comm_regist_timer);
            final TextView phone = findViewById(R.id.activity_regist_userId);
            final TextView code = findViewById(R.id.activity_regist_code);
            getPresenter().sendCode(this, phone, code, timer, button);
        });

        findViewById(R.id.regist_push).setOnClickListener(v -> {
            final TextView code = findViewById(R.id.activity_regist_code);
            final TextView phone = findViewById(R.id.activity_regist_userId);
            getPresenter().pushCode(this, phone, code);
        });
    }

    @Override
    public void initDataNet() {
    }

    @Override
    public void changeInputType(int editId, int imageId) {
        EditText input = findViewById(editId);
        if (null == input || input.getText().length() == 0) return;

        ImageView image = findViewById(imageId);
        if (null == image) return;

        boolean ok = (input.getTransformationMethod() == PasswordTransformationMethod.getInstance());
        image.setImageDrawable(getResources().getDrawable(ok ? R.drawable.ic_comm_regist_password_on : R.drawable.ic_comm_regist_password_off));
        input.setTransformationMethod(ok ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
        input.setSelection(input.getText().length());
    }

    @Override
    public void codeSucc() {

        EditText userId = findViewById(R.id.activity_regist_userId);
        EditText password1 = findViewById(R.id.activity_regist_password1);
        EditText password2 = findViewById(R.id.activity_regist_password2);
        boolean regist = NAME_REGIST.equals(getIntent().getStringExtra(NAME));
        getPresenter().regist(RegistActivity.this, userId, password1, password2, regist);
    }

    @Override
    public void warning(String text) {
        final WarningView warning = findViewById(R.id.activity_regist_warning);
        if (null == warning) return;
        warning.setWarning(text);
    }

    @Override
    public void goCompile() {
        setResult(RESULT_COMPILE);
        onBackPressed();
    }
}